package one.stand.zuul.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.embedded.undertow.UndertowBuilderCustomizer;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Administrator
 */
@Configuration
@ConditionalOnProperty(prefix = "one.env", name = "ssl", havingValue = "true", matchIfMissing = false)
public class HttpsConfig {
    // https 端口
    @Value("${server.port}")
    private Integer port;
//
//    /**
//     * spring boot 2.0
//     *
//     * @return
//     */
//    @Bean
//    public TomcatServletWebServerFactory servletContainer() {
//        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
//            @Override
//            protected void postProcessContext(Context context) {
//                SecurityConstraint constraint = new SecurityConstraint();
//                constraint.setUserConstraint("CONFIDENTIAL");
//                SecurityCollection collection = new SecurityCollection();
//                collection.addPattern("/*");
//                constraint.addCollection(collection);
//                context.addConstraint(constraint);
//            }
//        };
//        tomcat.addAdditionalTomcatConnectors(httpConnector());
//        return tomcat;
//    }
//
//    @Bean
//    public Connector httpConnector() {
//        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
//        connector.setScheme("http");
//        //Connector监听的http的端口号
//        connector.setPort(80);
//        connector.setSecure(false);
//        //监听到http的端口号后转向到的https的端口号
//        connector.setRedirectPort(443);
//        return connector;
//    }

    @Bean
    public ServletWebServerFactory servletContainer() {
        UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
        factory.addBuilderCustomizers((UndertowBuilderCustomizer) builder -> builder.addHttpListener(80, "0.0.0.0"));
//        factory.addDeploymentInfoCustomizers((UndertowDeploymentInfoCustomizer) deploymentInfo -> {
//            SecurityConstraint securityConstraint = new SecurityConstraint();
//            WebResourceCollection webResourceCollection = new WebResourceCollection();
//            webResourceCollection.addUrlPattern("/*");
//            securityConstraint.addWebResourceCollection(webResourceCollection);
//            securityConstraint.setTransportGuaranteeType(TransportGuaranteeType.CONFIDENTIAL);
//            securityConstraint.setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.PERMIT);
//            deploymentInfo.addSecurityConstraint(securityConstraint);
//            deploymentInfo.setConfidentialPortManager(exchange -> port);
//        });
        return factory;
    }
}
